<?php

namespace app\yunda\validate;
use think\Validate;
use think\Db;

/**
 * 用户地址数据验证
 * @package app\yunda\validate
 */
class Address extends Validate
{
    //定义验证规则
    protected $rule = [
        'name|姓名' => 'require|max:30',
        'phone|联系方式'  => 'require|regex:^1[3,5,6,7,8]{1}\d{9}$',
        'home_1|省市区'  => 'require',
        'address|详细地址'  => 'require',
        'openid|用户id'  => 'require|checkOpenid',

    ];

    //定义验证提示
    protected $message = [
        'name.max' => '姓名最多30个字符',
        'phone.regex' => '手机号码不正确',
    ];

    protected function checkOpenid($value,$rule)
    {
            $row=Db::table('dp_yd_user')->where('openid',$value)->find();
            if(empty($row)){
                return $rule='用户不存在';
            }
            return $rule=true;
    }
}
